﻿var cinema, cinemas = {};

$(function() {
	if (window.location.hash == "") {
		window.location.hash = $(".left-menu a:first").addClass("active").attr(
				"href");
		requestCinema();
	} else {
		$(".left-menu a").each(function() {
			if ($(this).attr("href") == window.location.hash) {
				$(this).addClass("active");
			} else {
				$(this).removeClass("active");
			}
		});
		requestCinema();
	}

	$("#galleryImgs")
			.on(
					"click",
					"img",
					function() {
						var popup = $(
								"<div align='center' class='j-window' title='"
										+ $("#galleryName").html() + "'>")
								.appendTo($(document.body));
						var src = $(this).attr("src");

						jRoot
								.dialog(
										popup,
										{
											close : function() {
												$(this).dialog('destroy')
														.remove();
											},
											minWidth : 700,
											minHeight : 350,
											modal : true,
											create : function() {
												var content = "<div id='movieSlide'>";
												content += "<div class='item'><img src='"
														+ src + "'/></div>";
												$("img", "#galleryImgs")
														.each(
																function() {
																	var data = $(
																			this)
																			.attr(
																					"src");
																	if (data != src) {
																		content += "<div class='item'><img src='"
																				+ data
																				+ "'/></div>";
																	}
																})
												content += "</div><div class='prev_movieSlide'></div><div class='next_movieSlide'></div>";
												popup.append(content);
											}
										});
						$("#movieSlide").scrollable( {
							'effect' : 'fade',
							axis : 'y'
						});
					});

	$("#divGmap div").click(function() {
		popupFile(cinema.name, cinema.gmap);
	});
	$("#divGmap").click(function() {
		popupFile(cinema.name, cinema.gmap);
	});

	$(".left-menu").on("click", "a", function() {
		if (!$(this).hasClass("active")) {
			$(".left-menu a.active").removeClass("active");
			$(this).addClass("active");
			window.location.hash = $(this).attr("href");
			requestCinema();
			void (0);
		}
	});
});

function requestCinema() {
	var op = cinemas[window.location.hash];
	if (op) {
		loadCinema(op);
	} else {
		$("#page").request("page", "cinema", {
			sendAll : false,
			busy : true,
			validate : function() {
				var valid = false;
				$(".left-menu a").each(function() {
					if ($(this).attr("href") == window.location.hash) {
						valid = true;
						return false;
					}
				});
				return valid;
			},
			params : {
				id : function() {
					return $.trim(window.location.hash).replace(/[^\d]/g, "");
				}
			}
		});
	}
}

function loadCinema(op) {
	cinemas[window.location.hash] = op;
	cinema = op;

	$("#imgMain").attr("src", op.media).attr("alt", op.name);
	$("#galleryName").html(op.name);
	$("#galleryAddress").html(op.address);
	$("#galleryTel").html(op.tel);
	if (op.gmap != null) {
		$("#divGmap").removeClass("hide").css("background-image",
				"url(" + op.gmap + ")");
	} else {
		$("#divGmap").addClass("hide");
	}

	$("#imgTicket").attr("src", op.ticket);
	$("#imgTicket3d").attr("src", op.ticket3d);
	$("#imgTicket3dHoliday").attr("src", op.ticket3dHoliday);

	var gallery = $("#galleryImgs").empty();
	if (op.medias != null) {
		var imgs = op.medias.split(",");
		var str = "";
		for ( var i = 0; i < imgs.length; i++) {
			str += "<div class='item'><img src='" + imgs[i] + "'/></div>";
		}
		gallery.html(str);
		gallery.scrollable( {
			size : 4,
			nextSize : 4
		});
	}
}

function popupFile(name, src) {
	var win = $("#popupImage"), h = 250, w = 300;
	if (win.attr("id")) {
		win.dialog("option", "title", name);
		var img = $("img", win);
		if (src != img.attr("src")) {
			img.bind("load", function(e) {
				$(this).show("slide", 100, function() {
					win.dialog("option", {
						position : "center"
					});
				});
				$(this).unbind(e);
			}).hide("slide", 100, function() {
				$(this).attr("src", src);
			});
		}
	} else {
		var win = $(
				"<div id='popupImage' align='center' title='" + name
						+ "' style='overflow:hidden;'>")
				.appendTo(document.body), i = $(
				"<img class='mauto pointer' src='" + src + "'>").load(
				function(e) {
					self.openPopup();
				}).error(function() {
			self.openPopup();
		}).appendTo(win.click(function() {
			win.dialog("close");
		}));
	}

	this.openPopup = function() {
		jRoot.dialog(win, {
			show : "fade",
			close : function() {
				win.dialog("destroy").remove();
			},
			width : "auto",
			height : "auto",
			maxWidth : "800px",
			maxHeight : "600px"
		});
	}
}